package com.itheima.pay.ali;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @authar by xiecong
 * @Date 2022/4/11
 * @Description
 */
@RestController
@RequestMapping("/alipay")
public class AliPayController {

    @GetMapping("/precreate/{out_trade_no}")
    public AlipayTradePrecreateResponse precreate(@PathVariable("out_trade_no") String out_trade_no){
        AlipayClient alipayClient = new DefaultAlipayClient(
                        "https://openapi.alipay.com/gateway.do",
                        "app_id",
                        "your private_key",
                        "json","GBK",
                        "alipay_public_key",
                        "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "out_trade_no");
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "黑马餐饮-消费");

        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println(response.getQrCode());
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return response;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
}
